<?php
/**
 * @filename ManageController.php
 * @author zouhao <zouhao@dodoca.net>
 * @datetime 2016/4/22 10:36
 * @description 会员管理
 */
class Member_ManageController extends SellerController
{

    /**
     * @author zouhao <zouhao@dodoca.net>
     * @description 标签列表
     * @datetime 2016年4月11日11:13:12
     */
    public function tagAction()
    {
        $page = intval($this->_request->getQuery('page', 1));
        $page = max($page, 1);
        $pageSize = 10;
        $where['status'] = 1;
        $sellerId = $this->getCurrentUser()->getSellerId();
        $where['seller_id'] = $sellerId;
        $tagModel = new U_TagModel();
        $list = $tagModel->listPage($where, array('*'), '', $page, $pageSize);
        $count = $tagModel->count;
        $totalPage = ceil($count/$pageSize);
        $data = array(
            'total' => $count,
            'page' => $page,
            'totalPage' => $totalPage
        );
        !empty($list) && $data['list'] = $list;
        $this->assign('loadModuleScript', true);
        $this->display($data, 'member/manage/tag.html');
    }

    /**
     * @author zouhao <zouhao@dodoca.net>
     * @description 编辑标签信息
     * @datetime 2016年4月11日11:13:12
     */
    public function editAction()
    {
        $data = array();
        if($this->_request->isPost()){
            $id = $this->_request->getPost('id');
            $tagModel = new U_TagModel();
            $data['info'] = $tagModel->find(array('id' => $id, 'status' => 1), array('*'));
        }
        $this->assign('loadModuleScript', true);
        $this->display($data, 'member/manage/edit.html');
    }

    /**
     * @author zouhao <zouhao@dodoca.net>
     * @description 保存编辑的标签信息
     * @datetime 2016年4月11日11:13:12
     */
    public function ajaxSaveAction()
    {
        if($this->_request->isXmlHttpRequest() && $this->_request->isPost())
        {
            try{
                $sellerId = $this->getCurrentUser()->getSellerId();
                $title = trim($this->_request->getPost('title'));
                $sum = trim($this->_request->getPost('sum'));
                $num = trim($this->_request->getPost('num'));
                $id = $this->_request->getPost('id');
                if(empty($title)){
                    throw new Exception('标签标题不能为空');
                }
                $tagModel = new U_TagModel();
                if($id){
                    $set = array(
                        'title' => $title,
                        'sum' => $sum,
                        'num' => $num
                    );
                    $res = $tagModel->updateData($set, array('id' => $id, 'status' => 1));
                    SellerLogService::getInstance()->log($this->getCurrentUser()->getSellerId(), $this->getCurrentUser()->getShopId(), $this->getCurrentUser()->get('id'), 2, 'member_manage', '更新会员标签设置');
                }
                else{
                    $set = array(
                        'seller_id' => $sellerId,
                        'title' => $title,
                        'sum' => $sum,
                        'num' => $num,
                        'create_time' => time(),
                        'status' => 1
                    );
                    $res = $tagModel->insert($set);
                    SellerLogService::getInstance()->log($this->getCurrentUser()->getSellerId(), $this->getCurrentUser()->getShopId(), $this->getCurrentUser()->get('id'), 1, 'member_manage', '新增会员标签设置');
                }
                if($res !== false){
                    $this->ajaxReturn(array(
                        'code' => 1,
                        'msg' => '保存成功'
                    ));
                }
                else{
                    throw new Exception('数据库错误,请稍后再试');
                }
            }
            catch(Exception $e){
                $this->ajaxReturn(array(
                    'code' => -1,
                    'msg' => $e->getMessage()
                ));
            }
        }
        else{
            $this->ajaxReturn(array(
                'code' => -1,
                'msg' => '非法访问'
            ));
        }
    }

    /**
     * @author zouhao <zouhao@dodoca.net>
     * @description 删除标签
     * @datetime 2016年4月11日11:13:12
     */
    public function deleteAction()
    {
        if($this->_request->isXmlHttpRequest() && $this->_request->isPost())
        {
            try{
                $id = $this->_request->getPost('id');
                if(empty($id)){
                    throw new Exception('缺少参数');
                }
                $tagModel = new U_TagModel();
                $res = $tagModel->deleteData(array('id' => $id, 'status' => 1));
                if($res === false){
                    throw new Exception('数据库错误,请稍后再试');
                }
                else{
                    SellerLogService::getInstance()->log($this->getCurrentUser()->getSellerId(), $this->getCurrentUser()->getShopId(), $this->getCurrentUser()->get('id'), 3, 'member_manage', '删除会员标签设置');
                    $this->ajaxReturn(array(
                        'code' => 1,
                        'msg' => '删除成功'
                    ));
                }
            }
            catch(Exception $e){
                $this->ajaxReturn(array(
                    'code' => -1,
                    'msg' => $e->getMessage()
                ));
            }
        }
        else{
            $this->ajaxReturn(array(
                'code' => -1,
                'msg' => '非法访问'
            ));
        }
    }

}